package cn.adkii.springframework.context.support;

import cn.adkii.springframework.beans.BeansException;
import cn.adkii.springframework.beans.factory.ConfigurableListableBeanFactory;
import cn.adkii.springframework.beans.factory.support.DefaultListableBeanFactory;

/**
 * @author malingdi
 * @description 获取Bean工厂和加载资源
 * @date 2023/11/23 18:38
 */
public abstract class AbstractRefreshableApplicationContext extends AbstractApplicationContext {
    private DefaultListableBeanFactory beanFactory;

    /**
     * 获取DefaultListableBeanFactory 的实例化以及对资源配置的加载操作loadBeanDefinitions(beanFactory),在加载完成后即可对
     * spring.xml配置文件中Bean对象的定义和注册，同时也包括了接口BeanFactoryPostProcessor、BeanPostProcessor的配置Bean信息。
     * @throws BeansException
     */
    @Override
    protected void refreshBeanFactory() throws BeansException {
        DefaultListableBeanFactory beanFactory = createBeanFactory();
        loadBeanDefinitions(beanFactory);
        this.beanFactory = beanFactory;
    }

    private DefaultListableBeanFactory createBeanFactory() {
        return new DefaultListableBeanFactory();
    }

    protected abstract void loadBeanDefinitions(DefaultListableBeanFactory beanFactory);

    @Override
    protected ConfigurableListableBeanFactory getBeanFactory() {
        return beanFactory;
    }
}
